# -*- coding:utf-8 -*-
import sys
import numpy as np
import os
import struct

if __name__ == "__main__":
    print 'Total:', len(sys.argv)
    print 'list:', str(sys.argv)

    path = os.getcwd()
    str_files = os.listdir(path)

    for name in str_files:
        print(name)
        str_name = name.split('.')
        if(len(str_name) != 2 or str_name[1] != "pcm"):
            continue
        filename = name
        saved_name = str_name[0] + ".txt"
        file = open(filename,"rb")
        file_pcm = open(saved_name,"w")
        data = file.read()
        print("\r\n-------------------------\r\n")
        print("Len:",len(data))
        for i in range(0,len(data) - 2,2):
            # print(data[0],data[1])
            
            str1 = data[i]+ data[i+1]
            # print("str1",str1)
            test = struct.unpack('<h',str1)
            # print("test",repr(test[0]))
            file_pcm.write(repr(test[0]))
            file_pcm.write(' ')
            

        # print("data1",ord(data[1]))
        # test = struct.unpack('>h',data)
        # print("test" , test[0])
        print("\r\n-------------------------\r\n")
        # # data_array = data.split(' ')
        # # print(data_array)
        # print("-------------------------")
        # # dig_data = map(int,data_array[3])
        # s_data = int(data_array[3],16)
        # print (int(data_array[3],16))
        # print (chr(s_data/256) + chr(s_data%256))
        # for ssstr in data_array:
        #     if(len(ssstr) > 0):
        #         s_data = int(ssstr,16)
        #         # file_pcm.write(chr((s_data  >> 24)& 0xff))
        #         # file_pcm.write(chr((s_data  >> 16)& 0xff))
        #         file_pcm.write(chr(s_data / (256) ))
        #         file_pcm.write(chr(s_data % (256) ))
        # # print(bytes(bytearray.fromhex(data_array[3])))
        # print("-------------------------")
        file.close()
        file_pcm.close()
